/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.consultorio.controle;

import br.com.consultorio.bean.Convenio;
import br.com.consultorio.dao.ConvenioDao;
import br.com.consultorio.dao.ConvenioDaoImp;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Thiago
 */
@WebServlet(name = "ServletConvenio", urlPatterns = {"/ServletConvenio"})
public class ServletConvenio extends HttpServlet {
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    private RequestDispatcher rd;
    private Convenio convenio;
    private ConvenioDao convenioDao;

    protected void processRequest()
            throws ServletException, IOException {
       String cmd = request.getParameter("cmd");
        switch (cmd) {
            case "cadCon":
                carregaConvenio();
                break;
            case "salvar":
                salvar();
                break;
            case "pesqNome":
                pesquisaNome();
                break;
            case "excluir":
                excluir();
                break;
            case "alterar":
                alterar();
                break;
            case "carregaConv":
                pesquisaConvPorId();
                break;
              
        }
        rd.forward(request, response);
    }

   private void salvar() {
        convenioDao = new ConvenioDaoImp();
        Convenio convenio = carregaConvenio();
        try {
            convenioDao.salvar(convenio);
            //manda e-mail com dados do usuário
            rd = request.getRequestDispatcher("admin/pesqConvenio.jsp");
        } catch (Exception e) {
            System.out.println("erro ao salvar convênio " + e.getMessage());
        }
    }
   
   private void pesquisaNome() {
        convenioDao = new ConvenioDaoImp();
        try {
            List<Convenio> convenios = convenioDao.pesquisaPorNome(request.getParameter("nome"));
            request.setAttribute("convenios", convenios);
            rd = request.getRequestDispatcher("admin/pesqConvenio.jsp");

        } catch (SQLException ex) {
            Logger.getLogger(ServletConvenio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ServletConvenio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    private Convenio carregaConvenio() {
        Convenio convenio = new Convenio();
        
        convenio.setNome(request.getParameter("nome"));
        convenio.setCobertura(request.getParameter("cobertura"));
        
        return convenio;
    }
    
    private Convenio carregaAltConvenio() {
        Convenio convenio = new Convenio();
        
        convenio.setId(Integer.parseInt(request.getParameter("id")));
        convenio.setNome(request.getParameter("nome"));
        convenio.setCobertura(request.getParameter("cobertura"));
        
        return convenio;
    }
    
    private void excluir(){
        int id = Integer.parseInt(request.getParameter("id"));
        convenioDao = new ConvenioDaoImp();
        try {
            convenioDao.excluir(id);
            rd = request.getRequestDispatcher("admin/pesqConvenio.jsp");
            //TODO montar mensagem de excluido com sucesso
        } catch (Exception ex) {
            Logger.getLogger(ServletConvenio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void alterar(){
       convenio = carregaAltConvenio();
       convenioDao = new ConvenioDaoImp();
        try {
            convenioDao.alterar(convenio);
            rd = request.getRequestDispatcher("admin/pesqConvenio.jsp");
            //TODO montar mensagem
        } catch (Exception ex) {
            Logger.getLogger(ServletConvenio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void pesquisaConvPorId(){
        convenioDao = new ConvenioDaoImp();
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            convenio = (Convenio) convenioDao.procurar(id);
            request.setAttribute("convenio", convenio);
            rd = request.getRequestDispatcher("admin/altConvenio.jsp");
        } catch (Exception ex) {
            Logger.getLogger(ServletConvenio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }
    
}
